package com.tubruk.trackme;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.NinePatch;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.NinePatchDrawable;
import android.location.Address;
import android.location.Geocoder;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class MyLocationOverlay extends Overlay {

	GeoPoint p;
	String text;

	/*public MyLocationOverlay(GeoPoint p, String text) {

		
	}
	*/
	
	public void setParameter(GeoPoint p, String text){
		this.p = p;
		this.text = text;
	}

	@Override
	public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
			long when) {

		Projection projection = mapView.getProjection();
		
		Point point = new Point();
		if (p != null) {
			projection.toPixels(p, point);
		}


		Paint paint = new Paint();
		paint.setARGB(255, 255, 255, 255);
		paint.setTextSize(paint.getTextSize() + 1f);
		paint.setTextAlign(Paint.Align.CENTER);
		paint.setARGB(255, 0, 0, 0);
		paint.setAntiAlias(true);
		
		float[] widths = new float[text.length()];
        int count = paint.getTextWidths(text, 0, text.length(), widths);
        
        float newWidth = 0;
        for (int i=0;i<count;i++){
        	newWidth = newWidth + widths[i];
        }
        
        //NinePatchDrawable ninepatch = (NinePatchDrawable) TrackMapActivity.getContext().getResources().getDrawable(R.drawable.bubble);
        //Drawable drawable = ninepatch.mutate();
        //drawable.setBounds(point.x, point.y, 0, 0);
        
        Bitmap bubble =  BitmapFactory.decodeResource(TrackMapActivity.getContext().getResources(), R.drawable.bubble);
        NinePatch bubble9p = new NinePatch(bubble, bubble.getNinePatchChunk(), null);
        //NinePatchDrawable bubble9pDrawable = new NinePatchDrawable(bubble9p);
//bubble9pDrawable.setBounds(point.x, point.y, point.x+ 300, point.y-300);
        

//Drawable d = bubble9pDrawable.mutate();

        Rect location = new Rect(point.x-(int) (newWidth / 2) -45, point.y-80, point.x+(int) (newWidth / 2) - 8, point.y-20);
  //      d.setBounds(location);
    //    d.draw(canvas);
        /*
        int width = bubble.getWidth();
        int height = bitmap.getHeight();

        int newHeight = 100;
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
    	
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		*/
        
        bubble9p.draw(canvas, location, paint);
        //bubble9pDrawable.draw(canvas);
        //ninepatch.draw(canvas);
		//canvas.drawBitmap(resizedBitmap, point.x, point.y, paint);


		canvas.drawText(text, point.x-25, point.y-50, paint);


		return super.draw(canvas, mapView, shadow, when); 
	}




}
